﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Command;
using Entity;
namespace DAL
{
    public class L_CommentDAL
    {
        //查询评论信息
        public static List<Comment> SelectComment()
        {
            string sql = string.Format("select*from Comment");
            SqlDataReader sdr = DBHelper.Getdata(sql);
            List<Comment> UI = new List<Entity.Comment>();
            while (sdr.Read())
            {
                Comment ct = new Comment();
                ct.CommentId = (int)sdr["CommentId"];
                ct.BookId = (int)sdr["BookId"];
                ct.UserId = (int)sdr["UserId"];
                ct.CommentValue = (string)sdr["CommentValue"];
                ct.CommentDate = (DateTime)sdr["CommentDate"];
                ct.CommentState = (int)sdr["CommentState"];
                UI.Add(ct);
            }
            sdr.Close();
            return UI;
        }
        public static void UpdateComment(Comment c)
        {
            string sql = string.Format("select * from Comment where CommentId='{0}'", c.CommentId);
            SqlDataReader sdr = DBHelper.Getdata(sql);
            int i = 0;
            while (sdr.Read())
            {
                i = (int)sdr["CommentState"];
            }
            sdr.Close();
            if (i == 1)
            {
                string sql1 = string.Format("update Comment set CommentState='2' where CommentId={0}", c.CommentId);
                DBHelper.Adddata(sql1);
            }
        }
        //查询订单信息
        public static List<OrdersandAddress> SelectOrder()
        {
            string sql = string.Format("select*from OrderInformatica  o  ,ShoppingAddress s where o.OrderAddress=s.AddressId and OrderState='1'");
            SqlDataReader sdr = DBHelper.Getdata(sql);
            List<OrdersandAddress> UI = new List<Entity.OrdersandAddress>();
            while (sdr.Read())
            {
                OrdersandAddress od = new OrdersandAddress();
                od.AddressId = (int)sdr["AddressId"];
                od.OrderId = (string)sdr["OrderId"];
                od.UserId = (int)sdr["UserId"];
                od.OrderAmount = (decimal)sdr["OrderAmount"];
                od.Name = (string)sdr["Name"];
                od.phone = (string)sdr["phone"];
                od.AddressValue = (string)sdr["AddressValue"];
                od.OrderState = (int)sdr["OrderState"];
                UI.Add(od);
            }
            sdr.Close();
            return UI;
        }
        //接收订单
        public static void UpdateOreder(OrderInformatica o)
        {
            string sql = string.Format("select*from OrderInformatica where OrderId='{0}'", o.OrderId);
            SqlDataReader sdr = DBHelper.Getdata(sql);
            int i = 0;
            while (sdr.Read())
            {
                i = (int)sdr["OrderState"];
            }
            sdr.Close();
            if (i == 1)
            {
                string sql1 = string.Format("update OrderInformatica set OrderState='2' where OrderId='{0}'", o.OrderId);
                DBHelper.Adddata(sql1);
            }
        }
        //拒绝订单
        public static void UpdateOreders(OrderInformatica o)
        {
            string sql = string.Format("select*from OrderInformatica where OrderId='{0}'", o.OrderId);
            SqlDataReader sdr = DBHelper.Getdata(sql);
            int i = 0;
            while (sdr.Read())
            {
                i = (int)sdr["OrderState"];
            }
            sdr.Close();
            if (i == 1)
            {
                string sql1 = string.Format("update OrderInformatica set OrderState='3' where OrderId='{0}'", o.OrderId);
                DBHelper.Adddata(sql1);
            }
        }
        //修改订单信息
        public static List<ShoppingAddress> UpdateOrederMessage(OrdersandAddress s)
        {
            string sql = string.Format("select*from ShoppingAddress  where AddressId='{0}'", s.AddressId);
            SqlDataReader sdr = DBHelper.Getdata(sql);
            List<ShoppingAddress> list = new List<ShoppingAddress>();
            while (sdr.Read())
            {
                ShoppingAddress p = new ShoppingAddress();
                p.AddressId = (int)sdr["AddressId"];
                p.UserID = (int)sdr["UserId"];
                p.Name = (string)sdr["Name"];
                p.AddressValue = (string)sdr["AddressValue"];
                p.phone = (string)sdr["phone"];
                list.Add(p);
            }
            return list;
        }
        //修改订单信息
        public static void UpdateShop(ShoppingAddress s)
        {
            string sql = string.Format("Update ShoppingAddress set Name='{0}',AddressValue='{1}',phone='{2}'", s.Name, s.AddressValue, s.phone);
            DBHelper.Adddata(sql);
        }
    }
}